package com.ophiux.config;

import org.apache.http.HttpHost;
import org.elasticsearch.action.admin.indices.create.CreateIndexRequest;
import org.elasticsearch.action.admin.indices.delete.DeleteIndexRequest;
import org.elasticsearch.client.RequestOptions;
import org.elasticsearch.client.RestClient;
import org.elasticsearch.client.RestHighLevelClient;
import org.elasticsearch.client.indices.GetIndexRequest;
import org.elasticsearch.common.xcontent.XContentType;
import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;

import java.io.IOException;


/**
 * @desc: 索引库操作
 * @author: hhl
 * @date：2023/12/23 22:01
 */
public class IndexTest {

    private RestHighLevelClient client;

    @Test
    public void test(){
        System.out.println(client);
    }


    //创建索引库
    @Test
    void testCreatedIndex() throws IOException {
        //1.创建Request对象
        CreateIndexRequest request = new CreateIndexRequest();

        //2.准备请求的参数：DSL语句
        request.source("", XContentType.JSON);

        //3.发送请求
        client.indices().create(request, RequestOptions.DEFAULT);
    }

    @Test
    void testDeleteIndex() throws IOException {
        //1.创建Request对象
        DeleteIndexRequest request = new DeleteIndexRequest("hhlindex");
        //2.发送请求
        client.indices().delete(request, RequestOptions.DEFAULT);
    }

    @Test
    void testExistsIndex() throws IOException {
        //1.创建Request对象
        GetIndexRequest request = new GetIndexRequest("hhlindex");
        //2.发送请求
        boolean exists = client.indices().exists(request, RequestOptions.DEFAULT);
        System.out.println(exists);
    }


    @BeforeEach
    public void init(){
        this.client = new RestHighLevelClient(RestClient.builder(
            new HttpHost("http://192.168.30.128", 9200)
        ));
    }
    @AfterEach
    public void close() throws IOException {
        this.client.close();
    }

}
